//
//  LLBBaesTableViewController.swift
//  LLBBaseVC
//
//  Created by Yun on 2018/11/1.
//

import UIKit
import MJRefresh
import LLBUtil
import DZNEmptyDataSet

open class LLBBaseTableViewController: LLBBaseViewController {

    private var emptyTitleString:String = ""
    
    @objc public var tableView:UITableView!
    
    override open func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override open func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    open func setTableViewWithType(style:UITableView.Style, edge: UIEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)) {
        if(tableView == nil){
            tableView = UITableView(frame: CGRect.zero, style: style)
            tableView.delegate = self
            tableView.dataSource = self
            tableView.backgroundColor = UIColor.hex(from: 0xFFF8F8F8)
            tableView.keyboardDismissMode = .onDrag
            tableView.separatorColor = UIColor.hex(from: 0xFFF8F8F8)
            view.addSubview(tableView)
            tableView.snp.makeConstraints({ (make) in
                make.left.equalTo(edge.left)
                make.right.equalTo(-edge.right)
                make.bottom.equalTo(-edge.bottom)
                make.top.equalTo(edge.top)
            })
        }
    }
}

// MARK: - TableView
extension LLBBaseTableViewController:UITableViewDataSource,UITableViewDelegate{
    
    // MARK: - TableView
    open func numberOfSections(in tableView: UITableView) -> Int {
        return 0
    }
    
    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return CGFloat.leastNormalMagnitude
    }
    
    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}

// MARK: - empty view
extension LLBBaseTableViewController:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate{
    open func showEmptyView(title:String = "暂无数据"){
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
        emptyTitleString = title
        tableView.reloadEmptyDataSet()
    }
    
    // MARK: - DZNEmptyDataSetSource
    @objc open func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let mu = NSMutableAttributedString.init(string: emptyTitleString)
        mu.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hex(from: 0xFF333333),
                          NSAttributedString.Key.font:llbMediumFont(size: 15)],
                         range: NSMakeRange(0, emptyTitleString.count))
        return mu
    }
    
    @objc open func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        let desc = "点击刷新"
        let mu = NSMutableAttributedString.init(string: desc)
        mu.addAttributes([NSAttributedString.Key.foregroundColor:UIColor.hex(from: 0xFF666666),
                          NSAttributedString.Key.font:llbFont(size: 13)],
                         range: NSMakeRange(0, desc.count))
        return mu
    }
    
    @objc open func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return ImagesHelper.default_empty_image
    }
    
    @objc open func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return -50.llbWF
    }
    
    // MARK: - DZNEmptyDataSetDelegate
    @objc open func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    @objc open func emptyDataSetShouldAllowTouch(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
    
    @objc open func emptyDataSet(_ scrollView: UIScrollView!, didTap view: UIView!) {
        self.headerRefreshAction()
    }
}

// MARK: - 上下拉刷新
extension LLBBaseTableViewController{
    //MARK:上下拉刷新
    open func showHeaderRefreshWithAction(){
        let header = MJRefreshNormalHeader(refreshingTarget: self, refreshingAction: #selector(headerRefreshAction))
        header?.lastUpdatedTimeLabel.isHidden = true
        header?.stateLabel.font = llbFont(size: 15)
        header?.stateLabel.textColor = UIColor.hex(from: 0xFF333333)
        self.tableView?.mj_header = header
    }
    
    open func showFooterRefreshWithAction(){
        let footer = MJRefreshAutoNormalFooter(refreshingTarget: self, refreshingAction: #selector(footerRefreshAction))
        self.tableView?.mj_footer = footer
        self.tableView.mj_footer.isHidden = true
    }
    
    @objc open func headerRefreshAction() {}
    @objc open func footerRefreshAction() {}
    
    open func stopRefresh() {
        if let header = self.tableView.mj_header,header.isRefreshing{header.endRefreshing()}
        if let footer = self.tableView.mj_footer,footer.isRefreshing{footer.endRefreshing()}
    }
}


